package com.itestor.iptv.modules.common.actions;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.vo.RegionManageBean;

/**
 * 
 * @author Mr.li
 * 
 */
public class RegionAutoSelectAction extends Action {
    
    /**
     * 
     */
    private static Map REGION_LEVEL_MAP=new HashMap<Integer, String>();
    
    static {        
        REGION_LEVEL_MAP.put(1, "⒈");
        REGION_LEVEL_MAP.put(2, "┖⒉");
        REGION_LEVEL_MAP.put(3, "─┶⒊");
        REGION_LEVEL_MAP.put(4, "──┶⒋");
        REGION_LEVEL_MAP.put(5, "───┶⒌");
        REGION_LEVEL_MAP.put(6, "────┶⒍");
        REGION_LEVEL_MAP.put(7, "─────┶⒎");        
    }

    /**
     * 业务对象.
     */
    private RegionManageService regionManageService;

    /**
     * 区局联动展现.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return actionForward
     * @throws Exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");        
        String type = request.getParameter("type");
        String keyValue = (String) request.getParameter("keyValue");
        StringBuffer sb = new StringBuffer("<state>");

        if ("regionId".equals(type)) {
            Collection<RegionManageBean> regionList = new ArrayList<RegionManageBean>();

            if (!StringUtils.isEmpty(keyValue)) {
                regionList = regionManageService.getRegionListByParentid(Integer
                        .parseInt(keyValue));
            }

            for(RegionManageBean region : regionList) {
                sb.append("<subregion><![CDATA[").append(region.getIdRegion())
                        .append(",").append(getMark(region.getRegionLevel())).append(region.getRegionName()).append(
                                "]]></subregion>");
            }
            
        }
        sb.append("</state>");
        
        
        
        System.out.println(sb.toString());
        PrintWriter out = response.getWriter();
        out.write(sb.toString());
        out.close();
        return null;
    }
    
    /**
     * 获取分级符号
     * @param level
     * @return
     */    
    private String getMark(int level) {
        StringBuilder  mark =new StringBuilder();
        mark.append(REGION_LEVEL_MAP.get(level+1));
        return mark.toString();
    }

    /**
     * 
     * @param pRegionManageService
     */
    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }

}
